TOP

Camel Case Function for LibreOffice Calc

YLC Utilities logo

CAMELCASE() Description

The function CAMELCASE() is designed to combine words with a variable case of letters, that is, a single word is returned as a result, for example "camelCaseFunction".

Camel Case splits a string of words separated by spaces, hyphens, underscores, or letter case into separate elements. Then, it converts all of these to uppercase words except the first one, which is converted to lowercase, and combines all the elements into one word.

The CAMELCASE() function can be useful in programming to convert variable names according to accepted code writing conventions (coding conventions) .


StarBASIC code for function Camel Case

Here is the macro code to create a custom function that can combine different words into one, where the logical parts will be separated by a variable case (Camel Case) in LibreOffice Calc.

Open the menu Tools - Macros - Edit Macros..., select Module1 and copy the following text into the module:

Function CamelCase(ByVal str As String) As String
  'moonexcel.com.ua
  Dim words As Variant
  Dim i     As Integer
  Dim FCalc As Object
  
  FCalc = CreateUnoService("com.sun.star.sheet.FunctionAccess")
  
  str = Replace(str,"-"," ")
  str = Replace(str,"_"," ")  
  str = FCalc.callFunction("TRIM", Array(str))  
  str = FCalc.callFunction("REGEX", Array(str,"([:lower:])([:upper:])","$1 $2","g"))
    
  str = LCase(str)
  words = Split(str, " ")
    
  For i = LBound(words) + 1 To UBound(words)       
    words(i) = UCase(Left(words(i), 1)) & Mid(words(i), 2)       
  Next i
    
  CamelCase = Join(words,"")
End Function

Then close Macro Editor, return to LibreOffice Calc and use our new function in any cell.

Using the extension

You can also use the feature CAMELCASE() by installing the free extension YouLibreCalc.oxt or its full-featured version YLC_Utilities.oxt .

After that, this function will be available in all files that will be opened in LibreOffice Calc.